//
//  NYTopicModel.m
//  百思不得姐
//
//  Created by Apple on 15/8/23.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYTopicModel.h"
#import "NYTopConment.h"
#import "NYTopUser.h"

#import <MJExtension.h>

@implementation NYTopicModel

+ (NSDictionary *)replacedKeyFromPropertyName
{
    return @{
             @"ID":@"id",
             @"top_cmt":@"top_cmt[0]",
             @"smallimage":@"image0",
             @"bigimage":@"image1",
             @"mediumimage":@"image2",
             @"cellType":@"type"
             };
}
- (CGFloat)cellHeight
{
    if (!_cellHeight) {
        //计算文字label的高度
        //文字的宽度
        CGFloat textMaxW = [UIScreen mainScreen].bounds.size.width - 2 *Margin;
        CGFloat textMaxH = [self.text boundingRectWithSize:CGSizeMake(textMaxW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height;
        
         _cellHeight = Margin + 35 + Margin + textMaxH ;
        
        //中间view
        if (self.cellType != NYTopicCellTypeText) {
            
            CGFloat conmentViewW = textMaxW;
            CGFloat conmentViewH = conmentViewW * self.height / self.width;
            /**
             *  图片过大，进行点击查看大图
             */
            if (self.cellType == NYTopicCellTypePicture && conmentViewH > NYScreenW) {
                conmentViewH = 200;
                self.bigPicture = YES;
            }
            CGFloat conmentViewX = Margin;
            CGFloat conmentViewY = _cellHeight + Margin;
            //中间内容的frame
            self.contentFrame = CGRectMake(conmentViewX, conmentViewY, conmentViewW, conmentViewH);
            _cellHeight +=  conmentViewH + Margin;
        }
        
        //最热评论
        if (self.top_cmt) {
             NSString *contentText = [NSString stringWithFormat:@"%@：%@",self.top_cmt.user.username,self.top_cmt.content];
    
            CGFloat conmentH = [contentText boundingRectWithSize:CGSizeMake(textMaxW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size.height;
            _cellHeight +=  17 + conmentH  + Margin;
        }
        
        _cellHeight +=Margin + toolBarHeight + Margin;
    }
    
    return _cellHeight;
}
- (NSString *)create_time
{
    static NSDateFormatter *dft;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dft = [[NSDateFormatter  alloc]init];
    });
    dft.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSDate *createdate = [dft dateFromString:_create_time];
    
    NSDateComponents *comps = createdate.intervalToNow;
    
    if (createdate.isThisYear) {
        if (createdate.isToday) {
//            if (comps.hour >=1) {
//                return [NSString stringWithFormat:@"%zd小时前",comps.hour];
//            } else if (comps.minute >=1){
//                return [NSString stringWithFormat:@"%zd分钟前",comps.hour];
//            } else {
//                return @"刚刚";
//            }
            if (comps.hour >= 1) { // interval >= 1小时  xx小时前
                return [NSString stringWithFormat:@"%zd小时前", comps.hour];
            } else if (comps.minute >= 1) { // 1小时 > interval >= 1分钟  xx分钟前
                return [NSString stringWithFormat:@"%zd分钟前", comps.minute];
            } else { // 1分钟 > interval  刚刚
                return @"刚刚";
            }

        } else if (createdate.isYesterday){
            dft.dateFormat = @"昨天 HH:mm:ss";
            return [dft stringFromDate:createdate];
        }else
        {
            dft.dateFormat = @"MM-dd HH:mm:ss";
            return [dft stringFromDate:createdate];
        }
    } else {
        return _create_time;
    }
}
- (CGRect)contentFrame
{
    [self cellHeight];
    return _contentFrame;
}
@end
